//
//  HomePageViewController.swift
//  CompareNew
//
//  Created by 唐先才 on 16/1/27.
//  Copyright © 2016年 唐先才. All rights reserved.
//

import Foundation
import UIKit

//typealias sendViewControllerClosure = (goViewController: UIViewController) -> Void
class HomePageViewController: UIViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
    
    var _button: UIButton?
    var _barView: UIView?
    var _willViewController: UIViewController?
    var _viewControllersArray: NSArray = []
    var _pvc: UIPageViewController?
//    var myClosure: sendViewControllerClosure?
    
//    func initWithClosure(closure: sendViewControllerClosure) {
//        myClosure = closure
//    }
    
    override func viewWillAppear(animated: Bool) {
        self.navigationController?.navigationBar.hidden = true
    }
    
    override func viewWillDisappear(animated: Bool) {
        self.navigationController?.navigationBar.hidden = false
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.whiteColor()
        self.createPageViewController()
        self.createNav()
    }
    
    func createPageViewController() {
        _pvc = UIPageViewController(transitionStyle: .Scroll, navigationOrientation: .Horizontal, options: nil)
        guard let _pvc = _pvc else {
            return
        }
        _pvc.view.frame = CGRect(x: 0, y: 64, width: WIDTH, height: HEIGHT - 64)
        _pvc.dataSource = self
        _pvc.delegate = self
        _pvc.view.backgroundColor = UIColor.whiteColor()
        self.view .addSubview(_pvc.view)
        let rvc = RecommendViewController()
        let bvc = BuyViewController()
        rvc.viewController = self
        _viewControllersArray = [rvc, bvc]
        _pvc.setViewControllers([rvc], direction: .Reverse, animated: false, completion: { (Bool) -> Void in
        })
//        if (myClosure != nil){
//            myClosure!(goViewController: self)
//        }
    }
    
    func createNav() {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: 2 * UIScreen.mainScreen().bounds.size.width, height: 64))
        view.userInteractionEnabled = true
        view.backgroundColor = UIColor.whiteColor()
        self.viewIfLoaded?.addSubview(view)
        let imgView = UIImageView(frame: view.frame)
        imgView.image = UIImage(named: "main_titleBar")
        imgView.userInteractionEnabled = true
        view.addSubview(imgView)
        let titleArray = ["推荐", "海淘"]
        for i in 0...1 {
            _button = UIButton(type: .System)
            guard let _button = _button else {
                return
            }
            _button.frame = CGRect(x: CGFloat(i) * WIDTH / 2, y: 20, width: WIDTH / 2, height: 36)
            _button.setTitle(titleArray[i], forState: .Normal)
            _button.setTitleColor(UIColor.whiteColor(), forState: .Normal)
            _button.titleLabel?.textAlignment = NSTextAlignment.Center
            _button.titleLabel?.font = UIFont.systemFontOfSize(20)
            _button.tag = 100 + i
            _button.addTarget(self, action: "clickAction:", forControlEvents: .TouchUpInside)
            imgView.addSubview(_button)
            
        }
        guard let button = self.view.viewWithTag(100) as? UIButton else {
            return
        }
        _button = button
        _barView = UIView(frame: CGRect(x: WIDTH / 6, y: 58, width: WIDTH / 6, height: 2))
        guard let _barView = _barView else {
            return
        }
        _barView.backgroundColor = UIColor.whiteColor()
        imgView.addSubview(_barView)
        
    }
    
    func clickAction(sender: UIButton) {
        guard let _pvc = _pvc else {
            return
        }
        guard let _barView = _barView else {
            return
        }
        if sender.tag == 101 {
            UIView.animateWithDuration(0.3, animations: { () -> Void in
                _barView.frame = CGRect(x: WIDTH * 2 / 3, y: 58, width: WIDTH / 6, height: 2)
            })
            guard let _viewControllers1 = _viewControllersArray[1] as? UIViewController else {
                return
            }
            _pvc.setViewControllers([_viewControllers1], direction: UIPageViewControllerNavigationDirection.Reverse, animated: false, completion: { (Bool) -> Void in
            })
        } else if (sender.tag == 100) {
            UIView.animateWithDuration(0.3, animations: { () -> Void in
                _barView.frame = CGRect(x: WIDTH / 6, y: 58, width: WIDTH / 6, height: 2)
            })
            guard let _viewControllers0 = _viewControllersArray[0] as? UIViewController else {
                return
            }
            _pvc.setViewControllers([_viewControllers0], direction: UIPageViewControllerNavigationDirection.Reverse, animated: false, completion: { (Bool) -> Void in
            })
        }
        _button = sender
    }
    
    func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
        if viewController.isKindOfClass(RecommendViewController) {
            return nil
        }
        guard let _viewControllers0 = _viewControllersArray[0] as? UIViewController else {
            return nil
        }
        return _viewControllers0
    }
    
    func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
        if viewController.isKindOfClass(BuyViewController){
            return nil
        }
        guard let _viewControllers1 = _viewControllersArray[1] as? UIViewController else {
            return nil
        }
        return _viewControllers1
    }
    
    func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
        guard let _barView = _barView else {
            return
        }
        if completed {
            if let _willViewController = _willViewController {
                if _willViewController.isKindOfClass(BuyViewController.self) {
                    UIView.animateWithDuration(0.3, animations: { () -> Void in
                        _barView.frame = CGRect(x: WIDTH * 2 / 3, y: 58, width: WIDTH / 6, height: 2)
                    })
                    guard let button = self.view.viewWithTag(101) as? UIButton else {
                        return
                    }
                    _button = button
                } else {
                    UIView.animateWithDuration(0.3, animations: { () -> Void in
                        _barView.frame = CGRect(x: WIDTH / 6, y: 58, width: WIDTH / 6, height: 2)
                    })
                    guard let button = self.view.viewWithTag(100) as? UIButton else {
                        return
                    }
                    _button = button
                }
            }
        }
    }
    
    func pageViewController(pageViewController: UIPageViewController, willTransitionToViewControllers pendingViewControllers: [UIViewController]) {
        _willViewController = pendingViewControllers[0]
    }
    
    
}